<?php
function findCommonPath($aPath,$bPath){
    $bPath=array(dirname($bPath));
    $arr1=explode('/',$aPath);
    $arr2=explode('/',$bPath[0]);
    $str='';
    for($i=1;$i<count($arr2);$i++){
        if($arr1[$i]==$arr2[$i]){
            $str.='/'.$arr1[$i];
        }
    }
    return $str;
}

print_r(findCommonPath('/a/b/c/d/test.php','/a/b/d/c/test.php'));













?>